package me.siphone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.tencent.mmkv.MMKV;
import me.siphone.Constants;
import me.siphone.http.api.LoginApi;
import me.siphone.sip.SipUtil;
import org.linphone.R;
import org.linphone.core.TransportType;

public class LoginActivity extends BaseActivity {

    private EditText etUsername;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);

        String username = MMKV.defaultMMKV().getString(Constants.USERNAME, "");
        String password = MMKV.defaultMMKV().getString(Constants.PASSWORD, "");

        if (!TextUtils.isEmpty(username)) {
            etUsername.setText(username);
        }

        if (!TextUtils.isEmpty(password)) {
            etPassword.setText(password);
        }

        findViewById(R.id.btn_login)
                .setOnClickListener(
                        view -> {
                            login();
                        });
    }

    private void login() {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        MMKV.defaultMMKV().putString(Constants.USERNAME, username);
        MMKV.defaultMMKV().putString(Constants.PASSWORD, password);

        EasyHttp.post(LoginActivity.this)
                .api(new LoginApi(username, password))
                .request(
                        new HttpCallbackProxy<LoginApi.Result>(LoginActivity.this) {
                            @Override
                            public void onHttpSuccess(@NonNull LoginApi.Result result) {
                                if (result.getCode() == 200) {
                                    MMKV.defaultMMKV()
                                            .putString(Constants.TOKEN, result.getToken());

                                    LoginApi.Result.Device device = result.getDevice();
                                    if (device != null) {
                                        SipUtil.createAccount(
                                                Constants.SIP_SERVER,
                                                TransportType.Tcp,
                                                device.getDeviceUid(),
                                                device.getPassword(),
                                                device.getDeviceName());

                                        SipUtil.setDefaultSettings();
                                    } else {
                                        Toast.makeText(
                                                        LoginActivity.this,
                                                        "未绑定软电话",
                                                        Toast.LENGTH_SHORT)
                                                .show();
                                    }

                                    startActivity(
                                            new Intent(LoginActivity.this, MainActivity.class));
                                    finish();
                                } else {
                                    Toast.makeText(
                                                    LoginActivity.this,
                                                    result.getMsg(),
                                                    Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }
                        });
    }
}
